dheffel@vitds2.nswc.navy.mil (David R. Heffelfinger) writes:
>Hi there,
> There is (hopefully) a simple answer to my question:
>I have some C code divided into different source files.
>When I try to compile using
> "cc file1.c file2.c file3.c"
>the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files? In other words, is there som equivalent to "extern" for constants?
Put those defines in a header file, and include the header file in
"file1.c", "file2.c" and "file3.c"? No, too simple :-). You could try
to use _real_ constants:
Declare your "constants" in _one_ translation unit as
const double PI = 3.141506;
Use
extern const double PI;
in all other translation units. This has some advantages when working
with a good debugger that _can_ inspect a const, but not a define.
The #define-directive is a preprocessor statement, and the proeprocessor
performs textual exchange of a "#defined" macro with it's definition.
The preprocessor does not create any constants that have any linkage
if you write
#define PI 3.141506
After preprocessing, the compiler does not distinguish between a
line that contains "PI" and the same line that contains "3.141506",
because both lines now contain "3.141506". "3.141506" is a floating
point literal, and all you can do with it is use it in an expression.